import java.util.*;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = -1;
        }

        for (int i = 0; i < m; i++) {
            int x = in.nextInt() - 1;
            int y = in.nextInt() - 1;
            int z = in.nextInt();
            // 如果两个元素都未确定
            if (a[x] == -1 && a[y] == -1) {
                if (z % 2 == 0) {
                    // 如果z是偶数，a[x]和a[y]要么都是0，要么都是1
                    // 先不做处理，等待后续线索
                } else {
                    // 如果z是奇数，a[x]和a[y]一个是0，一个是1
                    // 先不做处理，等待后续线索
                }
            } else if (a[x]!= -1) {
                // 如果a[x]已确定，根据z和a[x]确定a[y]
                a[y] = (z - a[x]) % 2;
            } else if (a[y]!= -1) {
                // 如果a[y]已确定，根据z和a[y]确定a[x]
                a[x] = (z - a[y]) % 2;
            }
        }

        // 第二次遍历线索，处理之前未处理的情况
        for (int i = 0; i < m; i++) {
            int x = in.nextInt() - 1;
            int y = in.nextInt() - 1;
            int z = in.nextInt();
            if (a[x] == -1 && a[y] == -1) {
                if (z % 2 == 0) {
                    // 如果z是偶数，且a[x]和a[y]都未确定
                    // 但之前可能有相关线索使其中一个可确定
                    if (a[x]!= -1) {
                        a[y] = a[x];
                    } else if (a[y]!= -1) {
                        a[x] = a[y];
                    }
                } else {
                    // 如果z是奇数，且a[x]和a[y]都未确定
                    // 但之前可能有相关线索使其中一个可确定
                    if (a[x]!= -1) {
                        a[y] = 1 - a[x];
                    } else if (a[y]!= -1) {
                        a[x] = 1 - a[y];
                    }
                }
            }
        }

        int count = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] == -1) {
                count++;
            }
        }
        System.out.println(count);
    }
}


/*public class Main {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
    }
    public static int[] a;
    public static int[] b;
    public static int n;
    public static int x;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        x = in.nextInt();
        a = new int[n];
        b = new int[n];
        for(int i = 0; i < n; i++) {
            a[i] = in.nextInt();
            b[i] = in.nextInt();
        }

        TreeNode root = new TreeNode(0);
        createTree(root, 0);
        boolean ret = checkLeaf(root);
        if(ret) {
            System.out.println("Yes");
        }else {
            System.out.println("No");
        }
    }

    private static boolean checkLeaf(TreeNode root) {
        if(root == null) {
            return false;
        }
        if(root.left == null && root.right == null && root.val == x) {
            return true;
        }
        return checkLeaf(root.left) || checkLeaf(root.right);
    }

    private static void createTree(TreeNode root, int index) {
        if(index == n) {
            return;
        }
        TreeNode cur1 = new TreeNode(root.val + a[index]);
        TreeNode cur2 = new TreeNode(root.val + b[index]);
        root.left = cur1;
        root.right = cur2;
        createTree(root.left, index+1);
        createTree(root.right, index+1);
    }
}*/
